package com.settlement.system.service.impl;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.common.util.SecurityUtils;
import com.settlement.system.common.util.common.SystemUserUtil;
import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblAcsUserRoleAuthorityMapper;
import com.settlement.system.mapper.TblRefundAuditBillDtlMapper;
import com.settlement.system.mapper.TblRefundAuditBillMapper;
import com.settlement.system.mapper.security.userdetails.SysUserDetails;
import com.settlement.system.model.entity.common.*;
import com.settlement.system.model.vo.TblRefundAuditBillVo;
import com.settlement.system.service.ITblRefundAuditBillService;
import jakarta.annotation.Resource;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * 退款稽核单据主表 服务实现类
 *
 * @author xiaochuan
 * @since 2020-06-28
 */
@Service
public class TblRefundAuditBillServiceImpl extends BaseServiceImpl implements ITblRefundAuditBillService {

    @Resource
    private TblRefundAuditBillMapper tblRefundAuditBillMapper;

    @Resource
    private TblRefundAuditBillDtlMapper tblRefundAuditBillDtlMapper;

    @Resource
    private TblAcsUserRoleAuthorityMapper tblAcsUserRoleAuthorityMapper;

    @Override
    public BaseMapper init() {
        return tblRefundAuditBillMapper;
    }

    @Override
    public int selectBillCount(Map<String, Object> params) throws ServiceException {
        try {
            return tblRefundAuditBillMapper.selectBillCount(params);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public List<TblRefundAuditBill> selectBillByPage(SinglePage singlePage, Map<String, Object> params, String orderByField, String orderBy) throws ServiceException {
        try {
            return tblRefundAuditBillMapper.selectBillByPage(singlePage, params, orderByField, orderBy);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public TblRefundAuditBillVo selectBillDtl(Map<String, Object> params) throws ServiceException {
        TblRefundAuditBillVo billVo = new TblRefundAuditBillVo();
        TblRefundAuditBillVo.RefundSendAndReturnVo refundSendAndReturnVo = new TblRefundAuditBillVo.RefundSendAndReturnVo();
        try {
            List<TblRefundAuditBill> billList = tblRefundAuditBillMapper.selectByParam(params);
            TblRefundAuditBill bill = billList.get(0);
            List<TblRefundAuditBillDtl> dtlList = tblRefundAuditBillDtlMapper.selectByParam(params);
            PropertyUtils.copyProperties(billVo, bill);
            PropertyUtils.copyProperties(refundSendAndReturnVo, bill);
            billVo.setTblRefundAuditBillDtl(dtlList);
            billVo.setRefundSendAndReturnVo(refundSendAndReturnVo);
            //查询单据下用户是否拥有操作权限
            SysUserDetails user = SecurityUtils.getUser();;
            params.put("authorityNo", "auth_002");
            params.put("loginName", user.getUsername());
            List<TblAcsUserRoleAuthority> allIngo = tblAcsUserRoleAuthorityMapper.selctAllUserByBillNo(params);
            if (CollectionUtils.isEmpty(allIngo)) {
                billVo.setAuth(false);
            } else {
                billVo.setAuth(true);
            }
            billVo.setResponsiblePerson(user.getUsername());
        } catch (Exception e) {
            throw new ServiceException(e);
        }
        return billVo;
    }

    @Override
    public void getDataBillByHandler(Map<String, Object> params, ResultHandler<TblRefundAuditBill> handler) throws ServiceException {
        try {
            tblRefundAuditBillMapper.getDataBillByHandler(params, handler);
        } catch (Exception ex) {
            throw new ServiceException(ex);
        }
    }

    @Override
    public void getDataBillAndDtlByHandler(Map<String, Object> params, ResultHandler<TblRefundAuditBill> handler) throws ServiceException {
        try {
            tblRefundAuditBillMapper.getDataBillAndDtlByHandler(params, handler);
        } catch (Exception ex) {
            throw new ServiceException(ex);
        }
    }
}

